package com.bdqn.crm;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.mapper.UserMapper;
import com.bdqn.crm.pojo.User;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class UserMapperTester {
    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById() {
        User user = userMapper.selectById(2L);
        System.out.println("usrName" + user.getUsrName());
    }

    @Test
    public void testInsert() {
        userMapper.insert(new User("MyBatis-Plus", "123456", 9L, 1));
    }

    @Test
    public void testFindAll() {
        List<User> list = userMapper.selectList(null);
        for (User user : list) {
            System.out.println("userName" + user.getUsrName());
        }
    }

    @Test
    public void testFind() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_name", "MyBatis-Plus");
        wrapper.eq("usr_password", "123456");
        List<User> list = userMapper.selectList(wrapper);
        if (list != null) {
            for (User user : list) {
                System.out.println("userName" + user.getUsrId());
            }
        }
    }

    @Test
    public void testPagination() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("usr_role_id", 2L);
        Page<User> page = new Page<User>(1, 2);
        IPage<User> userPage = userMapper.selectPage(page, wrapper);
        System.out.println("总记录数：" + userPage.getTotal());
        System.out.println("总页数：" + userPage.getPages());
        System.out.println("当前页数：" + userPage.getCurrent());
        System.out.println("每页记录数：" + userPage.getSize());
        System.out.println("当前记录：");
        for (User user : userPage.getRecords()) {
            System.out.println("usrId：" + user.getUsrId());
        }
    }
}
